<?php

namespace App\Admin\Models;

use App\Admin\Models\SystemModel;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorshipBibleChapter extends Model
{
    use SoftDeletes;

    protected $guarded = []; 

    public function __construct(){
        parent::__construct();
        $this->mergeCasts(SystemModel::fieldCasts($this->getTable()));
    }

    /**
     * 解析
     * @param string $scope
     * @return array $result
     */
    function parse($scope)
    {
        // 章节ID的范围
        $bcIds = [];
        // 卷ID的范围
        $bvIds = [];
        // 截取的范围，按分号分割
        $scopes = parseScope($scope);
        foreach ($scopes as $key => $value) {
            $tmpResult = $this->$key($value);
            foreach ($tmpResult['bc'] as $v) {
                 array_push($bcIds,$v);
            }
            foreach ($tmpResult['bv'] as $v) {
                 array_push($bvIds,$v);
            }
        }
        $result['bv'] = ['id',array_unique($bvIds)];
        $result['bc'] = ['id',array_unique($bcIds)];
        return $result;
    }
    /**
     * 解析卷范围
     * @param array $scopes
     * @return array
     */
    function volume($scopes)
    {
        $bv = new WorshipBibleVolume();
        // 范围
        $scope = implode(';', $scopes);
        // 简称
        $shortNames = $bv->parseShortName($scope);
        // 卷ID的范围
        $bvIds = [];
        foreach ($shortNames as $key => $value) {
            $tmpIds = $bv->_ids($value);
            foreach ($tmpIds as $v2) {
                array_push($bvIds, $v2);
            }   
        }
        // 章节ID的范围
        $bcIds = $this->whereIn('volume_id',$bvIds)->pluck('id');
        return ['bv'=>$bvIds,'bc'=>$bcIds];
    }

    /**
     * 解析章范围
     * @param array $scopes
     * @return array
     */
    function chapter($scopes)
    {
        $bv = new WorshipBibleVolume();
        // 章序号的范围
        $chapterIds = [];
        // 节序号的范围
        $verseIds = [];
        // 范围
        $scope = implode(';', $scopes);
        // 简称
        $shortNames = $bv->parseShortName($scope);
        foreach ($scopes as $key => $value) {
            $chapterIds[$key] = [];
            $verseIds[$key] = [];
            foreach (explode(',', $value) as $k => $v) {
                $chapterIds[$key][$k] = parseChapterId($v);
            }
        }
        foreach ($chapterIds as $key => $value) {
            foreach ($value as $k => $v) {
                if (!in_array($k, array_keys($shortNames[$key]))) {
                    unset($chapterIds[$key][$k]);
                }
            }
            $chapterIds[$key] = array_values($chapterIds[$key]);
        }
        // 章节ID的范围
        $bcIds = [];
        // 卷ID的范围
        $bvIds = [];
        foreach ($shortNames as $key => $value) {
            $appendBvIds = $bv->_ids($value);
            if (count($value) == 1) {
                $appendBcIds = $this->_ids(
                    current($value),
                    current($chapterIds[$key])
                );
            } else {
                $appendBcIds = [];
                foreach ($value as $k => $v) {
                    $appendBcTmpIds = $this->_ids(
                        $v,
                        $chapterIds[$key][$k]
                    );
                    foreach ($appendBcTmpIds as $v2) {
                        array_push($appendBcIds, $v2);
                    }
                }
                if (!strstr($scopes[$key], ';')) {
                    $appendBcIds = fill_number($appendBcIds);
                }
            }
            // 附加章节ID的范围
            foreach ($appendBcIds as $v) {
                array_push($bcIds,$v);
            }
            // 附加卷ID的范围
            foreach ($appendBvIds as $v) {
                array_push($bvIds,$v);
            }
        }
        return ['bv'=>$bvIds,'bc'=>$bcIds];
    }

    /**
     * 解析节范围
     * @param array $scopes
     * @return array
     */
    function verse($scopes)
    {
        $bv = new WorshipBibleVolume();
        // 章序号的范围
        $chapterIds = [];
        // 节序号的范围
        $verseIds = [];
        // 范围
        $scope = implode(';', $scopes);
        // 简称
        $shortNames = $bv->parseShortName($scope);
        // 章节ID的范围
        $bcIds = [];
        // 卷ID的范围
        $bvIds = [];
        // 最外面的范围
        $outermostScope = getOutermost($scope);
        foreach ($scopes as $key => $value) {
            $chapterIds[$key] = [];
            $verseIds[$key] = [];
            $outermostScope = getOutermost($value);
            $verseIds[$key] = getInner($outermostScope);
            if (strstr($outermostScope, ';')) {
                if (strstr($outermostScope, '[')) {
                    foreach (explode(';', $outermostScope) as $k => $v) {
                        $chapterIds[$key][$k] = parseChapterId($v);
                    }
                } else {
                    $chapterIds[$key][0] = parseChapterId($value);
                }
            } else {
                if (strstr($outermostScope, ':')) {
                    foreach (explode(',', $value) as $k => $v) {
                        $chapterIds[$key][$k] = parseChapterId($v);
                    }
                    $verseIds[$key] = getInner($value);
                } else {
                    if (strstr($outermostScope, '[')) {
                        foreach (explode(',', $outermostScope) as $k => $v) {
                            $chapterIds[$key][$k] = parseChapterId($v);
                        }
                    } else {
                        $chapterIds[$key][0] = parseChapterId($value);
                    }
                }
            }
        }
        // 章节ID的范围
        $bcIds = [];
        // 卷ID的范围
        $bvIds = [];
        foreach ($shortNames as $key => $value) {
            $appendBvIds = $bv->_ids($value);
            if (count($value) == 1) {
                if (count($chapterIds[$key]) == 1) {
                    if (strstr($verseIds[$key][0], ';')) {
                        $appendBcIds = [];
                        foreach (explode(';', $verseIds[$key][0]) as $v) {
                            $v = fill_number(explode(',', $v));
                            $appendBcTmpIds = $this->_ids(
                                current($value),
                                current($chapterIds[$key]),
                                $v
                            );
                            foreach ($appendBcTmpIds as $k2 => $v2) {
                                array_push($appendBcIds, $v2);
                            }
                        }
                    } else {
                        $appendBcIds = $this->_ids(
                            current($value),
                            current($chapterIds[$key]),
                            explode(',', $verseIds[$key][0])
                        );
                        $appendBcIds = fill_number($appendBcIds);
                    }
                } else {
                    $appendBcIds = [];
                    foreach ($chapterIds[$key] as $k => $v) {
                        $appendBcTmpIds = $this->_ids(
                            current($value),
                            $v,
                            explode(',', $verseIds[$key][$k])
                        );
                        if (strstr($verseIds[$key][$k], ',')) {
                            $appendBcTmpIds = fill_number($appendBcTmpIds);
                        }
                        foreach ($appendBcTmpIds as $v2) {
                            array_push($appendBcIds, $v2);
                        }
                    }
                    if (!strstr($scopes[$key], ';')) {
                        $appendBcIds = fill_number($appendBcIds);
                    }
                }
            } else {
                $appendBcIds = fill_number($this->_ids(
                    $value,
                    $chapterIds[$key],
                    $verseIds[$key]
                ));
                $appendBvIds = fill_number($appendBvIds);
            }
            // 附加章节ID的范围
            foreach ($appendBcIds as $v) {
                array_push($bcIds,$v);
            }
            // 附加卷ID的范围
            foreach ($appendBvIds as $v) {
                array_push($bvIds,$v);
            }
        }
        return ['bv'=>$bvIds,'bc'=>$bcIds];
    }

    /**
     * 根据卷章节范围获取起止id
     * @param mixed $shortNames
     * @param mixed $chapterIds
     * @param array $verseIds
     * @return array $ids
     */
    public function _ids($shortNames,$chapterIds,$verseIds=null)
    {
        $ids = [];
        $map = [];
        if (is_array($chapterIds)) {
            foreach ($shortNames as $key => $value) {
                $shortNames && $map['bv.short_name'] = $value;
                $chapterIds && $map['bc.chapter_id'] = $chapterIds[$key];
                $verseIds && $map['bc.verse_id'] = $verseIds[$key];

                $tmpIds = $this->from('worship_bible_chapters as bc')
                    ->withTrashed()
                    ->join('worship_bible_volumes as bv','bv.id','=','bc.volume_id')
                    ->where($map)
                    ->pluck('bc.id');
                foreach ($tmpIds as $v) {
                    array_push($ids,$v); 
                }
            }
        } else {
            $shortNames && $map['bv.short_name'] = $shortNames;
            $chapterIds && $map['bc.chapter_id'] = $chapterIds;
           
            $tmpIds = $this->from('worship_bible_chapters as bc')
                ->withTrashed()
                ->join('worship_bible_volumes as bv','bv.id','=','bc.volume_id')
                ->where($map);

            if ($verseIds) {
                $tmpIds->whereIn('bc.verse_id',$verseIds);
            }
            
            $tmpIds = $tmpIds->pluck('bc.id');

            foreach ($tmpIds as $v) {
                array_push($ids,$v); 
            }
        }
        sort($ids);
        return $ids;
    }
}
